package com.scau.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.scau.dto.CBECReturn;
import com.scau.exception.FileUtilException;
import com.scau.handleException.ResultCode;
import com.scau.pojo.Goods;
import com.scau.service.IGoodsService;
import com.scau.util.JsonUtil;
//import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("")
public class GoodsController {

    @Autowired
    IGoodsService goodsService;


    @RequestMapping(value = "/goods",method = RequestMethod.POST,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn addGoods(Goods goods, @RequestParam("img")MultipartFile file) throws IOException {

        goodsService.addBrandGoods(goods,file);

        //返回品牌商商品目录页面
        return CBECReturn.ok();
    }

    @RequestMapping(value = "/goods/{id}",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn deleteGoods(@PathVariable("id") String id) throws FileUtilException {
        Goods goods = goodsService.deleteBrandGoods(id);

        //返回品牌商商品目录页面
        return CBECReturn.ok(goods);
    }


    @RequestMapping(value = "/goods/{id}",method = RequestMethod.POST,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn updateGoods(@PathVariable("id") String id,Goods goods,@RequestParam(value = "img",required = false)MultipartFile file){


//        System.out.println("GoodsController.updateGoods:" + id);
        goods.setId(id);
        System.out.println("GoodsController.updateGoods:" + goods);
        Goods goods1 = goodsService.updateBrandGoods(goods,file);

        /*CBECReturn cbecReturn;
        if(file == null){
            cbecReturn = CBECReturn.ok(goods);
        }else{
            cbecReturn = CBECReturn.build(ResultCode.REQUESTBODY_ERROR);
        }
*/

        return CBECReturn.ok();
    }


    @RequestMapping(value = "/goods/{username}/{goodsName}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn getGoodsByName(@PathVariable("username") String username,@PathVariable("goodsName") String goodsName) {
        //异步刷新
        List<Goods> goodsList= goodsService.getBrandGoodsByName(username,goodsName);

        return CBECReturn.ok(goodsList);

    }

    @RequestMapping(value = "/goods/username/{username}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn getGoodsByUsername(@PathVariable("username") String username){
        List<Goods> goodsList = goodsService.getBrandGoodss(username);

        return CBECReturn.ok(goodsList);
    }

    @RequestMapping(value = "/goods",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn getAllGoods(){
        List<Goods> goodsList = goodsService.getAllGoodss();

        return CBECReturn.ok(goodsList);
    }


    @RequestMapping(value = "/goods/id/{id}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn getGoodsById(@PathVariable("id") String id){
        Goods goods = goodsService.getBrandGoods(id);
        return CBECReturn.ok(goods);
    }
}
